<template>
  <div class="app-container" :style="'height:' + height + 'px'">
    <div style="width: 100%; height: 100%" v-html="poHtmlCode"></div>
    <input type="hidden" name="fileId" id="fileId" />
    <input type="hidden" name="fileName" id="fileName" />
    <input type="hidden" name="reportId" id="reportId" />
  </div>
</template>

<script setup name="Index">
import { viewForm } from "@/api/report/reportProject";
const search = window.location.search;
const poHtmlCode = ref("");
const height = ref("");
const fileForm = ref({});
const bs = ref(0);

// const { proxy } = getCurrentInstance();

if (search) {
  const searchList = search.substring(1).split("&");
  fileForm.value = {
    fileId: searchList[0].split(":")[1],
    fileName: searchList[1].split(":")[1],
    reportId: searchList[2].split(":")[1],
  };
  viewForm(fileForm.value).then((res) => {
    poHtmlCode.value = res;
  });
}

function OnPageOfficeCtrlInit() {
  // PageOffice的初始化事件回调函数，您可以在这里添加自定义按钮
  pageofficectrl.AddCustomToolButton("保存", "Save(1)", 1);
  pageofficectrl.AddCustomToolButton(
    "发起检测报告审批流程",
    "handleInitiate",
    3
  );
}
function AfterDocumentOpened() {
  // PageOffice的文档打开后事件回调函数
  pageofficectrl.DisableSave = true;
}
function BeforeDocumentSaved() {
  // PageOffice的文档保存前事件回调函数
}
function AfterDocumentSaved() {
  // PageOffice的文档保存后事件回调函数
  if (pageofficectrl.CustomSaveResult == "ok") {
    if (bs.value == 2) {
      window.external.close();
      var sResult = window.external.CallParentFunc("tolc()");
      if (sResult !== "poerror:parentlost") {
        window.external.CallParentFunc("tolc()");
      }
      return
    }
    alert("保存成功！");
  } else {
    alert("保存失败！");
  }
}

function Save(bs1) {
  bs.value = bs1 ? bs1 : 0;
  document.getElementById("fileId").value = fileForm.value.fileId;
  document.getElementById("fileName").value = fileForm.value.fileName;
  document.getElementById("reportId").value = fileForm.value.reportId;
  pageofficectrl.WebSave();
}

function handleInitiate() {
  Save(2);
}

onMounted(() => {
  window.Save = Save;
  window.handleInitiate = handleInitiate;
  // 以下的为PageOffice事件的回调函数，名称不能改，否则PageOffice控件调用不到
  window.OnPageOfficeCtrlInit = OnPageOfficeCtrlInit;
  window.AfterDocumentOpened = AfterDocumentOpened;
  window.BeforeDocumentSaved = BeforeDocumentSaved;
  window.AfterDocumentSaved = AfterDocumentSaved;
  var box = document.querySelector(".app-container"); // 根据你的盒子ID选择器进行修改
  var boxDistanceFromTop = box.getBoundingClientRect().top; // 盒子距离顶部的距离
  height.value = window.innerHeight - boxDistanceFromTop - 21; // 视窗口高度减去盒子距离顶部的距离
});
</script>

<style scoped lang="scss"></style>
